import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pingouin as pg
import pyCompare

def bland_altman_plot(data1, data2, *args, **kwargs):
    data1     = np.asarray(data1)
    data2     = np.asarray(data2)
    mean      = np.mean([data1, data2], axis=0)
    diff      = data1 - data2                   # Difference between data1 and data2
    md        = np.mean(diff)                   # Mean of the difference
    sd        = np.std(diff, axis=0)            # Standard deviation of the difference

    plt.scatter(mean, diff, *args, **kwargs)
    plt.axhline(md,           color='gray', linestyle='--')
    plt.axhline(md + 1.96*sd, color='red')
    plt.axhline(md - 1.96*sd, color='red')

if __name__ == '__main__':
    name_list = ['LSLA','LSA','SIA','SHA']
    # data_pred = pd.read_csv('label_file\lumbar\pred_calculate_0602.csv')
    # data_label = pd.read_csv('label_file\lumbar\label_calculate_0602.csv')
    # data = pd.merge(data_pred,data_label,on='name')
    # data = data.sample(frac=0.5)
    # data.to_csv('label_file/lumbar/merge_calculate.csv',index=False)
    data = pd.read_csv('label_file/lumbar/merge_calculate.csv')
    name = name_list[3]
    data_x = np.array(data[name+'_x'])
    data_y = np.array(data[name+'_y'])

    error = [data_x-data_y]
    error = [abs(p) for p in error]
    print(np.mean(error),np.median(error))
    ax = pg.plot_blandaltman(data_x,data_y,)
    # bland_altman_plot(LSLA_x,LSLA_y)
    plt.title(name)
    plt.show()

